Author: Charles Tapley Hoyt
Estimated Run Time: 1 minute
This notebook outlines the process of parsing the JSON Graph File format used in the Causal Biological Network (CBN) Database.
In [1]:
import json
import requests
import os
import time
import networkx as nx
import pybel
from pybel.constants import *
import pybel_tools
from pybel_tools.visualization import to_jupyter
In [2]:
pybel.__version__
Out[2]:
In [3]:
pybel_tools.__version__
Out[3]:
In [4]:
time.asctime()
Out[4]:
In [5]:
res = requests.get("http://causalbionet.com/Networks/GetJSONGraphFile?networkId=hox_2.0_hs").json()
The structure is traversed, and the BELParser is manipulated directly. Normally, during BEL compilation, the usage of this class is hidden from the user.
In [6]:
graph = pybel.BELGraph()
parser = pybel.parser.BelParser(graph)
In [7]:
def get_citation(evidence):
return {
CITATION_NAME: evidence['citation']['name'],
CITATION_TYPE: evidence['citation']['type'],
CITATION_REFERENCE: evidence['citation']['id']
}
In [8]:
annotation_map = {
'tissue': 'Tissue',
'disease': 'Disease',
'species_common_name': 'Species'
}
In [9]:
species_map = {
'human': '9606',
'rat': '10116',
'mouse': '10090'
}
In [10]:
annotation_value_map = {
'Species': species_map
}
In [11]:
for edge in res['graph']['edges']:
for evidence in edge['metadata']['evidences']:
if 'citation' not in evidence or not evidence['citation']:
continue
parser.control_parser.clear()
parser.control_parser.citation = get_citation(evidence)
parser.control_parser.evidence = evidence['summary_text']
d = {}
if 'biological_context' in evidence:
annotations = evidence['biological_context']
if annotations['tissue']:
d['Tissue'] = annotations['tissue']
if annotations['disease']:
d['Disease'] = annotations['disease']
if annotations['species_common_name']:
d['Species'] = species_map[annotations['species_common_name'].lower()]
parser.control_parser.annotations.update(d)
bel = '{source} {relation} {target}'.format_map(edge)
try:
parser.parseString(bel)
except Exception as e:
print(e, bel)
In [12]:
to_jupyter(graph)
Out[12]:
In [17]:
pybel.to_database(graph)
In [ ]:
pybel.get_ver
This pipeline is implemented directly in PyBEL at pybel.from_cbn_jgif
In [13]:
with open(os.path.join(os.environ['BMS_BASE'], 'cbn', 'Human-2.0', 'Hox-2.0-Hs.jgf')) as f:
graph_jgif_dict = json.load(f)
In [14]:
%%time
graph = pybel.from_cbn_jgif(graph_jgif_dict)
In [15]:
bel_lines = pybel.to_bel_lines(graph)
graph_reloaded = pybel.from_lines(bel_lines)
In [16]:
to_jupyter(graph_reloaded)
Out[16]:
In [ ]: